package com.etc.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Users;
import com.etc.service.UsersService;
import com.etc.service.impl.UsersServiceImpl;
import com.etc.tools.PageData;
import com.etc.tools.ResponseData;
import com.google.gson.Gson;

/**
 * Servlet implementation class UsersController
 */
@WebServlet("/users.do")
public class UsersController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private UsersService usersService = new UsersServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");

		String op = "query";

		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}

		if ("query".equals(op)) {
			doQuery(request, response);
		} else if ("updateState".equals(op)) {
			doUpdateState(request, response);
		} else if ("update".equals(op)) {
			doUpdate(request, response);
		}
	}

	protected void doUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置响应内容
		response.setContentType("application/json;charset=utf-8");
		// 前端传递过来参数是json格式怎么接收? 此时我们需要通过流来处理BufferedReader
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String line = null;
		// stringbuffer存储传递的参数
		StringBuffer sb = new StringBuffer();
		while ((line = br.readLine()) != null) {
			// 读到的数据存储起来
			sb.append(line);
		}
		System.out.println("sb: " + sb);
		// 急需一个方法将json格式的字符串 转换 User对象 ->Gson
		Gson gson = new Gson();
		//String - >Users 
		Users user = gson.fromJson(sb.toString(), Users.class);
		System.out.println("user: " + user);
		// 1 调用service完成查询
		ResponseData resdata = usersService.update(user);
		String result = gson.toJson(resdata);
		// 3 将数据打印返回
		response.getWriter().print(result);
	}

	protected void doUpdateState(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置响应内容
		response.setContentType("application/json;charset=utf-8");
		int userid = Integer.parseInt(request.getParameter("userid"));
		int userstate = Integer.parseInt(request.getParameter("userstate"));
		// 1 调用service完成查询
		ResponseData resdata = usersService.updateState(userid, userstate);

		Gson gson = new Gson();
		String result = gson.toJson(resdata);
		// 3 将数据打印返回
		response.getWriter().print(result);
	}

	protected void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置响应内容
		response.setContentType("application/json;charset=utf-8");
		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}

		int pageNo = 1;

		int pageSize = 10;
		if (request.getParameter("page") != null) {
			pageNo = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("limit") != null) {
			pageSize = Integer.parseInt(request.getParameter("limit"));
		}
		// 1 调用service完成查询
		PageData<Users> pd = usersService.getUsers(keywords, pageNo, pageSize);

		// 2将返回结果转换为json对象 =>ResponseData
		ResponseData<Users> responseData = new ResponseData<Users>(0, "操作成功", pd.getTotal(), pd.getData());

		Gson gson = new Gson();
		String result = gson.toJson(responseData);
		// 3 将数据打印返回
		response.getWriter().print(result);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
